﻿Module commons

    Public ReadOnly MOD_APPSELF As String = "eWinSync"
    Public ReadOnly MOD_LOGGER As String = "Activities Logger"
    Public ReadOnly MOD_LOGGER_SETTING As String = "Activities Logger Setting"
    Public ReadOnly MOD_MYSQL_SETTING As String = "MySql Setting"
    Public ReadOnly MOD_WINCC_SETTING As String = "WinCC Setting"
    Public ReadOnly MOD_OPERATION_SETTING As String = "Operation Setting"
    Public ReadOnly MOD_MAPPING As String = "Mapping Definition"
    Public ReadOnly MOD_MOCKWINCC As String = "Wincc Mock"
    Public ReadOnly MOD_WINCCHANDLER As String = "Wincc Handler"
    Public ReadOnly MOD_MYSQLHANDLER As String = "Mysql Handler"
    Public ReadOnly MOD_SERVICEHANDLER As String = "EwService Handler"
    Public ReadOnly TAB_LOGS As String = "logs"
    Private ReadOnly DATEFORMAT As String = "dd-MM-yyyy HH:mm:ss"
    Private mobNotifyIcon As NotifyIcon
    Private WithEvents mobContextMenu As ContextMenu
    Private errormnu As MenuItem
    Private startservicemnu As MenuItem
    Private service As EwinsynServiceHandler
    Private servicegui As Laucher
    Private log As ActivityLogger
    Public Function pathExist(ByVal path As String) As Boolean
        Return False
    End Function
    Public Function fileExist(ByVal path As String) As Boolean
        Return False
    End Function
    Public Function dateToString(ByVal dt As Date, Optional ByVal sep As String = "") As String
        Dim c As String = ""
        c = dt.ToString("yyyy" + sep + "MM" + sep + "dd")
        Return c
    End Function
    Public Function todayAsString() As String
        Dim cm As String = Now.ToString(DATEFORMAT)
        Return cm
    End Function
    Public Function getTimestamp() As Long
        Return Today.Ticks
    End Function
    Public Function yesNoCancelMsg(ByVal lmod As String, ByVal msg As String) As MsgBoxResult
        Dim frm As New yesnotcancel
        frm.lblmodule.Text = lmod
        frm.txtmsg.Text = msg
        frm.ShowDialog()
        Return frm.DialogResult
    End Function
    Public Sub simpleMessage(ByVal lmod As String, ByVal msg As String, ByVal ico As MsgBoxStyle)
        MsgBox(msg, ico, lmod)
    End Sub
    Public Function toInteger(ByVal str As String, Optional ByVal defval As Integer = 0) As Integer
        Dim i As Integer = defval
        Try
            i = CInt(str)
        Catch ex As Exception
            i = 0
        End Try
        Return i
    End Function
    Public Function toInteger(ByVal str As TextBox, Optional ByVal defval As Integer = 0) As Integer
        Return toInteger(str.Text, defval)
    End Function
    Public Function toDate(ByVal dt As DateTime) As DateTime
        Dim d As New DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second)
        Return d
    End Function
    Public Sub main()
        log = ActivityLogger.getInstance()
        log.log(MOD_APPSELF, "EwinSync Application started", ActivityLoggerConf.LOGLEVEL_INFO)
        prepareSystray()
        service = EwinsynServiceHandler.getInstance
        mobNotifyIcon.Visible = True
        Application.Run()
    End Sub
    Private Sub prepareSystray()
        mobNotifyIcon = New NotifyIcon
        mobNotifyIcon.Visible = False
        mobNotifyIcon.BalloonTipTitle = "EWinSync"
        mobNotifyIcon.BalloonTipText = "Wincc Tag To Mysql Sync. Service"
        CreateMenu()
        mobNotifyIcon.ContextMenu = mobContextMenu
        mobNotifyIcon.Icon = New Icon("TimeMachine.ico")
    End Sub
    Private Sub showSetting()
        If (IsNothing(servicegui)) Then servicegui = New Laucher
        If (servicegui.IsDisposed) Then
            servicegui = Nothing
            servicegui = New Laucher
        End If
        servicegui.Show()
    End Sub
    Public Sub startService()
        log.log(MOD_APPSELF, "EwinSync Service Starting", ActivityLoggerConf.LOGLEVEL_INFO)
        errormnu.Text = "No Error"
        Dim ret As Integer = service.startService
        Select Case ret
            Case EwinsynServiceHandler.SERVICE_ERROR
                errormnu.Text = "Errors during launch"
                log.log(MOD_APPSELF, "EwinSync Service not started due to errors:" + service.getErrors, ActivityLoggerConf.LOGLEVEL_INFO)
            Case EwinsynServiceHandler.SERVICE_STARTED
                startservicemnu.Enabled = False
        End Select
        log.log(MOD_APPSELF, "EwinSync Service Starting Done", ActivityLoggerConf.LOGLEVEL_INFO)
    End Sub
    Public Sub stopService()
        log.log(MOD_APPSELF, "EwinSync Service Stopping", ActivityLoggerConf.LOGLEVEL_INFO)
        service.stopService()
        log.log(MOD_APPSELF, "EwinSync Service Stopping Done", ActivityLoggerConf.LOGLEVEL_INFO)
    End Sub
    Public Sub restartService()
        log.log(MOD_APPSELF, "EwinSync Service Restarting", ActivityLoggerConf.LOGLEVEL_INFO)
        service.restartService()
        log.log(MOD_APPSELF, "EwinSync Service restart Done", ActivityLoggerConf.LOGLEVEL_INFO)
    End Sub
    Private Sub exitapp()
        log.log(MOD_APPSELF, "EwinSync Application Exit", ActivityLoggerConf.LOGLEVEL_INFO)
        ActivityLogger.getInstance.closeLog()
        Application.Exit()
    End Sub

    Private Sub CreateMenu()
        Try
            errormnu = New MenuItem("No Error", New EventHandler(AddressOf showSetting))
            startservicemnu = New MenuItem("Start Service", New EventHandler(AddressOf startService))
            mobContextMenu = New ContextMenu
            mobContextMenu.MenuItems.Add(New MenuItem("Show", New EventHandler(AddressOf showSetting)))
            mobContextMenu.MenuItems.Add(New MenuItem("-"))
            mobContextMenu.MenuItems.Add(startservicemnu)
            mobContextMenu.MenuItems.Add(New MenuItem("Stop Service", New EventHandler(AddressOf stopService)))
            mobContextMenu.MenuItems.Add(New MenuItem("Restart Service", New EventHandler(AddressOf restartService)))
            mobContextMenu.MenuItems.Add("-")
            mobContextMenu.MenuItems.Add(errormnu)
            mobContextMenu.MenuItems.Add(New MenuItem("Exit", New EventHandler(AddressOf exitapp)))

        Catch obEx As Exception
            Throw obEx
        End Try
    End Sub
End Module
